import { Card, message, Row, Col, Space } from 'antd';
import { history } from 'umi';
import ProForm from '@ant-design/pro-form';
import type { FC } from 'react';
import { PageContainer } from '@ant-design/pro-layout';
import { queryArticleDetail, updateArticle } from './service';

import RichText from '@/components/RichText';

const BasicForm: FC<Record<string, any>> = () => {

    const onFinish = async (values: Record<string, any>) => {
        console.log('values', values);
        if (history.location.query.id) {
            values.id = history.location.query.id;
            try {
                const response = await updateArticle(values);
                if (response.code == 200) {
                    message.success('修改成功');
                    history.push('/article/article-list');
                } else {
                    message.error('修改失败!');
                }
            } catch (error) {
                message.error(error);
                console.log(error);
            }
        }
    };

    return (
        <PageContainer content="">
            <Card bordered={false}>
                <ProForm
                    hideRequiredMark
                    style={{ margin: 'auto', marginTop: 8 }}
                    name="basic"
                    layout="horizontal"
                    // initialValues={data}
                    onFinish={onFinish}
                    request={async () => {
                        if (history.location.query.id) {
                            return queryArticleDetail(history.location.query).then(({ data }) => {
                                console.log('data', data);
                                return data;
                            });
                        } else {
                            return {};
                        }
                    }}
                    submitter={{
                        render: (props, doms) => {
                            return (
                                <Row>
                                    <Col span={14} offset={4}>
                                        <Space>{doms}</Space>
                                    </Col>
                                </Row>
                            );
                        },
                    }}
                >
                    <ProForm.Item label="" name="content">
                        <RichText />
                    </ProForm.Item>
                </ProForm>
            </Card>
        </PageContainer >
    );
};

export default BasicForm;
